﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using BadmintonApp.Entity;
using BadmintonApp.DAO.Interface;

namespace BadmintonApp.DAO.Implement
{
    public class RechargeDAO : BaseDAO, IRechargeDAO
    {

        private int GetMaxRechargeID()
        {
            string sql = "Select IsNull(Max(FRechargeID), 0) from TRecharges";
            DbCommand selectCmd = db.GetSqlStringCommand(sql);
            return (int)db.ExecuteScalar(selectCmd);
        }


        #region IRechargeDAO Members

        /// <summary>
        /// 新增充值信息
        /// </summary>
        /// <param name="rechargeInfo"></param>
        public void InsertRecharge(TRechargeInfo rechargeInfo)
        {
            StringBuilder sbSQL = new StringBuilder();
            sbSQL.Append("Insert into TRecharges(FRechargeID, FRechargeUser, FMoney, FDate, FReceiver, FMemo) ");
            sbSQL.Append(" Values (@FRechargeID, @FRechargeUser, @FMoney, @FDate, @FReceiver, @FMemo)");
            DbCommand insertCmd = db.GetSqlStringCommand(sbSQL.ToString());

            rechargeInfo.FRechargeID = GetMaxRechargeID() + 1;
            db.AddInParameter(insertCmd, "FRechargeID", DbType.Int32, rechargeInfo.FRechargeID);
            db.AddInParameter(insertCmd, "FRechargeUser", DbType.Int32, rechargeInfo.FRechargeUser);
            db.AddInParameter(insertCmd, "FMoney", DbType.Decimal, rechargeInfo.FMoney);
            db.AddInParameter(insertCmd, "FDate", DbType.DateTime, rechargeInfo.FDate);
            db.AddInParameter(insertCmd, "FReceiver", DbType.Int32, rechargeInfo.FReceiver);
            db.AddInParameter(insertCmd, "FMemo", DbType.String, rechargeInfo.FMemo);
            db.ExecuteNonQuery(insertCmd);
        }

        /// <summary>
        /// 更新充值信息
        /// </summary>
        /// <param name="rechargeInfo">充值信息类</param>
        public void UpdateRecharge(TRechargeInfo rechargeInfo)
        {
            StringBuilder sbSQL = new StringBuilder();
            sbSQL.Append("Update TRecharges Set FRechargeUser=@FRechargeUser, FMoney=@FMoney, FDate=@FDate, ");
            sbSQL.Append(" FReceiver=@FReceiver, FMemo=@FMemo Where FRechargeID=@FRechargeID");
            DbCommand updateCmd = db.GetSqlStringCommand(sbSQL.ToString());
            db.AddInParameter(updateCmd, "FRechargeID", DbType.Int32, rechargeInfo.FRechargeID);
            db.AddInParameter(updateCmd, "FRechargeUser", DbType.Int32, rechargeInfo.FRechargeUser);
            db.AddInParameter(updateCmd, "FMoney", DbType.Decimal, rechargeInfo.FMoney);
            db.AddInParameter(updateCmd, "FDate", DbType.DateTime, rechargeInfo.FDate);
            db.AddInParameter(updateCmd, "FReceiver", DbType.Int32, rechargeInfo.FReceiver);
            db.AddInParameter(updateCmd, "FMemo", DbType.String, rechargeInfo.FMemo);
            db.ExecuteNonQuery(updateCmd);
        }

        /// <summary>
        /// 删除充值信息
        /// </summary>
        /// <param name="rechargeID">充值ID</param>
        public void DeleteRecharge(int rechargeID)
        {
            string sql = "Delete from TRecharges Where FRechargeID=@FRechargeID";
            DbCommand deleteCmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(deleteCmd, "FRechargeID", DbType.Int32, rechargeID);
            db.ExecuteNonQuery(deleteCmd);
        }

        /// <summary>
        /// 获取所有充值信息
        /// </summary>
        /// <returns>充值信息类列表</returns>
        public DataSet GetAllRecharge()
        {
            string sql = "Select * from TRecharges";
            DbCommand selectCmd = db.GetSqlStringCommand(sql);
            DataSet dsRecharge = db.ExecuteDataSet(selectCmd);
            return dsRecharge;
        }

        public DataSet GetRechargeByFilter(string sFilter)
        {
            return new DataSet();
        }

        #endregion
    }
}
